<?php

/**
 * This is the model class for table "tbl_adv_category".
 *
 * The followings are the available columns in table 'tbl_adv_category':
 * @property integer $id
 * @property string $title
 *
 * The followings are the available model relations:
 * @property TblAdv[] $tblAdvs
 * @property TblAdvTypeCategory[] $tblAdvTypeCategories
 */
class AdvCategory extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return AdvCategory the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_adv_category';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title', 'required'),
			array('title', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'Advs' => array(self::HAS_MANY, 'Adv', 'category_id'),
			'AdvTypeCategories' => array(self::HAS_MANY, 'AdvTypeCategory', 'category_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Title',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public static function dataTree()
    {
        $dataTree=array();
        $style = '';
        $expand = false;
        $model = AdvCategory::model()->findAll();
        foreach($model as $item)
        {
            if(isset($_GET['pid']) AND $_GET['pid']==$item->id)
            {
                $style = 'color:red;';
                $expand = true;
            }
            else
            {
                $style = '';
                $expand = false;
            }
            $dataTree[$item->id]=
                array(
                'text'=> '<a href="'.Yii::app()->createUrl('/adv/category',array('pid'=>$item->id)).'" style='.$style.'>'.$item->title.'</a>',
                'expanded'=>$expand
            );
            $mmodel = AdvTypeCategory::model()->findAll(array('condition'=>'category_id=:cid','params'=>array(':cid'=>$item->id)));
            foreach($mmodel as $iitem)
            {
                $d[]=
                  array('text'=>$iitem->title)
                ;
            }
            $dataTree[$item->id]['children'] = $d;
            $d = null;
        }

          /*  array(
                'text'=>'Grampa', //must using 'text' key to show the text
                'children'=>array(//using 'children' key to indicate there are children
                    array(
                        'text'=>'Father',
                        'children'=>array(
                            array('text'=>'me'),
                            array('text'=>'<a href="">big sis</a>'),
                            array('text'=>'little brother','url'=>'/asd'),
                        )
                    ),
                    array(
                        'text'=>'Uncle',
                        'children'=>array(
                            array('text'=>'Ben'),
                            array('text'=>'Sally'),
                        )
                    ),
                    array(
                        'text'=>'Aunt',
                    )
                )
            )
        );*/
        return $dataTree;
    }
}